package com.flux.ui.adapter

import android.content.Intent
import android.support.annotation.LayoutRes
import android.support.v7.widget.RecyclerView
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup

import com.flux.ui.activity.FBaseActivity
import java.lang.ref.WeakReference

/**
 * 用于Activity的RecycleAdapter
 * Created on 2017/11/7.
 *
 * @author yang
 */
abstract class FActivityRecycleAdapter<T : FBaseActivity>(var mActivity: WeakReference<T>) : RecyclerView.Adapter<RecyclerView.ViewHolder>() {

    protected fun startActivity(intent: Intent) {
        mActivity.get()?.startActivity(intent)
    }

    protected fun startActivity(cls: Class<*>, vararg extras: String) {
        mActivity.get()?.startActivity(cls, *extras)
    }

    protected fun startActivityForResult(cls: Class<*>, requestCode: Int, vararg extras: String) {
        mActivity.get()?.startActivityForResult(cls, requestCode, *extras)
    }

    protected fun setClick(view: View, listener: (v: View) -> Unit) {
        if (!view.hasOnClickListeners()) view.setOnClickListener(listener)
    }

    /**
     * 获取item布局
     * @param layout 资源文件
     * @param parent 父级控件
     * @return item布局
     */
    protected fun getItemView(@LayoutRes layout: Int, parent: ViewGroup) = LayoutInflater.from(mActivity.get()).inflate(layout, parent, false)!!
}